# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Contributor: Khorne <khorne AT khorne DOT me>

pkgbase=drone
pkgname=(
  'drone'
  'drone-oss'
)
pkgver=2.18.0
pkgrel=1
pkgdesc='Continuous Delivery platform built on container technology'
arch=('loong64' 'x86_64')
url='https://drone.io'
makedepends=('git' 'go')
options=('!lto')
_commit='15819f259e5ae40edf4311bd2e3bfd127d68af5b'
source=(
  "$pkgbase::git+https://github.com/harness/drone.git#commit=$_commit"
  'systemd.service'
  'tmpfiles.conf'
  'sysusers.conf'
)
sha512sums=('SKIP'
            '44b29c1388ea6cd0a064c1384f3224d164a37f9ca85fdb956ffafa1ddbcb11f3f01f91701d3b8ce341a593cd85a52cbf102f6549d3c722d6ff8f8eb9cc572ab1'
            'dfdc522b0e17f6b68d86f6599020bc75328de84aa1a393c53a0edc5ed229fb6ef99a0b544eebca6f9eee1b9f2ce04906bcca105792bccf2e49cdd51f10026886'
            '9488ba861487ecaf96e6b39afc3aeabd0951b9c0ee552cdb552d12f08f73cdcf2c57e5a322139f34e0aa5b2c9b2f5751f08bf66dca2bd8dd041b60b96e41c710')
b2sums=('SKIP'
        'ed1d5aebd207997ae2a523786eab0c6a3500080a294864ed5eb6e6251a827ef85a8b2c3bbfdc0ce41bb2f67e14dc65ae627c595688eabb4d822df2f4c971a449'
        '9c1bf8e4a382190f2d1bd560ef5c4a9fadad8c38dcd3cf3b79af42f6d0c16de533ba2d5f6cf676003e4ca6b745833e5e100baf4328111cf540ac5f43f67c6145'
        '18e5c4ea19348479dc3408bd2546f324f0908acc759c7c79c350006c6c5e6db02cd388a9aa9bd3d4f6b4b1f2efcf6aa7f7e67eac43f8d55566afe341b7ec142e')

pkgver() {
  cd "$pkgbase"
  git describe --tags | sed "s/^v//"
}

prepare() {
  cd "$pkgbase"

  # create directory for build output
  mkdir build build-oss

  # download dependencies
  export GOPATH="${srcdir}"
  go mod download
}

build() {
  cd "$pkgbase"

  # set Go flags
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export GOPATH="${srcdir}"
  local COMMON_FLAGS=(
    -v
    -buildmode=pie
    -mod=readonly
    -modcacherw
    -ldflags "-compressdwarf=false \
    -linkmode external \
    -extldflags ${LDFLAGS}"
  )

  # enterprise version
  go build \
    "${COMMON_FLAGS[@]}" \
    -o build \
    ./cmd/...

  # community version
  go build \
    "${COMMON_FLAGS[@]}" \
    -tags "oss nolimit" \
    -o build-oss \
    ./cmd/...
}

check() {
  cd "$pkgbase"
  go test -v ./...
}

package_drone() {
  pkgdesc+=" (Enterprise Edition)"
  license=('custom:Drone-Non-Commercial')
  # ref: https://discourse.drone.io/t/exec-runner-404-error-on-ping/7922
  depends=('glibc')
  conflicts=('drone-oss')

  # systemd integration
  install -vDm644 systemd.service "$pkgdir/usr/lib/systemd/system/$pkgname.service"
  install -vDm644 sysusers.conf "$pkgdir/usr/lib/sysusers.d/$pkgname.conf"
  install -vDm644 tmpfiles.conf "$pkgdir/usr/lib/tmpfiles.d/$pkgname.conf"

  cd "$pkgbase"

  # binary
  install -vDm755 -t "$pkgdir/usr/bin" build/*

  # license
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENSE NOTICE
}

package_drone-oss() {
  pkgdesc+=" (Community Edition)"
  license=('Apache')
  depends=('glibc' 'docker')
  conflicts=('drone')

  # systemd integration
  install -vDm644 systemd.service "$pkgdir/usr/lib/systemd/system/$pkgname.service"
  install -vDm644 sysusers.conf "$pkgdir/usr/lib/sysusers.d/$pkgname.conf"
  install -vDm644 tmpfiles.conf "$pkgdir/usr/lib/tmpfiles.d/$pkgname.conf"

  cd "$pkgbase"

  # binary
  install -vDm755 -t "$pkgdir/usr/bin" build-oss/*
}
